const qs = require('querystring');

let getParams = (ctx) => {
    return new Promise((resolve) => {
        let str = '';            //post
        ctx.req.on('data',(chunk) => {
            str += chunk;
        })

        ctx.req.on('end',() => {
            if(str.indexOf('{') != -1){
                //axios
                resolve(JSON.parse(str));//axios  {"username":"zs123","ticket":"123"}  对象字符串
            }else{
                //postman
                resolve(qs.parse(str));  //postman post传递的参数试序列化字符串  username=lisi%3D%3D%3D&ticket=K666
            }
        })
    }) 
}

module.exports = () => {
    return async (ctx,next) => {
        //接收前端post传递的参数
        if(ctx.request.method === 'GET'){
            //get
            await next();
        }else{
            ctx.request.body = await getParams(ctx);
            await next();
        }
    }
}